package com.gl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gl.entity.EduComment;
import com.gl.entity.UcenterMember;
import com.gl.service.EduCommentService;
import com.gl.service.UcenterMemberService;
import com.gl.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/comment")
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterMemberService memberService;

    //分页显示当前课程评论，按时间排序
    @GetMapping("getListByCourseId/{courseId}/{current}/{size}")
    public Page<EduComment> getListByCourseId(@PathVariable String courseId,
                                              @PathVariable long current,
                                              @PathVariable long size){
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByDesc("gmt_create");
        Page<EduComment> page = new Page<>(current,size);
        commentService.page(page, wrapper);

        return page;
    }


    //添加课程评论
    @PostMapping("saveComment")
    public String saveComment(@RequestBody EduComment eduComment, HttpServletRequest request){
        //获取用户信息
        //获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        //最终添加评论的对象
        EduComment comment = new EduComment();
        comment.setCourseId(eduComment.getCourseId());//课程id
        comment.setTeacherId(eduComment.getTeacherId());//讲师id
        comment.setMemberId(memberId);//评论用户id
        comment.setNickname(member.getNickname());//评论用户昵称
        comment.setAvatar(member.getAvatar());//评论用户的头像
        comment.setContent(eduComment.getContent());//评论内容
        commentService.save(comment);
        return null;
    }


}

